package com.yanqu.road.dao.impl.activity.tongbing;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.tongbing.data.TongBingUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class TongBingUserDataDaoImpl extends TempDataObjectDao<TongBingUserData> {

    public static String getTongBingUserDataSql = "select * from `t_u_activity_tongbing_data` where `activityId`=? and `userId`=?";

    public TongBingUserData getTongBingUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getTongBingUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateTongBingUserDataSql = "update `t_u_activity_tongbing_data` set worshipTime=?,positionId=?,campId=?,zhanGong=?,skinExchangeMap=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(TongBingUserData tongBingUserData) {
        tongBingUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, tongBingUserData.getWorshipTime()));
        para.put(i++, new DbParameter(Types.INTEGER, tongBingUserData.getPositionId()));
        para.put(i++, new DbParameter(Types.INTEGER, tongBingUserData.getCampId()));
        para.put(i++, new DbParameter(Types.BIGINT, tongBingUserData.getZhanGong()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(tongBingUserData.getSkinExchangeMap())));
        para.put(i++, new DbParameter(Types.INTEGER, tongBingUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, tongBingUserData.getUserId()));
        if (getDb().executeNoneQuery(updateTongBingUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertTongBingUserDataSql = "insert into `t_u_activity_tongbing_data`(activityId,userId,worshipTime,positionId,campId,zhanGong,skinExchangeMap) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(TongBingUserData tongBingUserData) {
        tongBingUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, tongBingUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, tongBingUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, tongBingUserData.getWorshipTime()));
        para.put(i++, new DbParameter(Types.INTEGER, tongBingUserData.getPositionId()));
        para.put(i++, new DbParameter(Types.INTEGER, tongBingUserData.getCampId()));
        para.put(i++, new DbParameter(Types.BIGINT, tongBingUserData.getZhanGong()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(tongBingUserData.getSkinExchangeMap())));
        if (getDb().executeNoneQuery(insertTongBingUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected TongBingUserData parseData(ResultSet rs) throws SQLException {
        TongBingUserData tongBingUserData = new TongBingUserData();
        tongBingUserData.setActivityId(rs.getInt("activityId"));
        tongBingUserData.setUserId(rs.getLong("userId"));
        tongBingUserData.setWorshipTime(rs.getLong("worshipTime"));
        tongBingUserData.setPositionId(rs.getInt("positionId"));
        tongBingUserData.setCampId(rs.getInt("campId"));
        tongBingUserData.setZhanGong(rs.getLong("zhanGong"));
        tongBingUserData.setSkinExchangeMap(JSON.parseObject(rs.getString("skinExchangeMap"), new TypeReference<Map<Integer, Integer>>() {
        }));
        return tongBingUserData;
    }

}
